package shaka.ui

import groovy.json.JsonBuilder
import groovy.json.JsonSlurper

import org.apache.commons.lang3.StringUtils
import org.eclipse.swt.SWT
import org.eclipse.swt.widgets.DirectoryDialog
import org.eclipse.swt.widgets.Text

import shaka.groovy.swt.SwtBuilder
import shaka.ui.utils.MkLinkUtils
import shaka.utils.SkUtils

class SwtMklinkPanel {
	private static FUNC = 'SwtMklinkPanel'
	static panel(SwtBuilder swt,String fontType,int fontSize,fontStyle){
		Map w = [:]
		Closure saveCB = { String value ->
			Text linked = w['3-t1']
			Text source = w['3-t2']

			Map args = [linked:linked.text, source:source.text]

			JsonBuilder jb = new JsonBuilder()
			jb(args)
			FuncStorage.instance.func(FUNC, value, jb.toString())
		}
		swt.cTabItem(c:[SWT.NONE], text:'路徑連結'){
			font(c:[fontType, fontSize, fontStyle])
			composite(c:[SWT.BORDER]){
				formLayout()
				w['1-lbl'] = label(c:[SWT.NONE], text:'路徑:'){
					font(c:[fontType, fontSize, fontStyle])
					formData(top:[0, 5], left:[0, 5])
				}
				w['1-btn'] = button(c:[SWT.PUSH], text:'選擇 ...'){
					font(c:[fontType, fontSize, fontStyle])
					formData(top:[0, 1], right:[100, -5])
					event(click:{
						DirectoryDialog fd = swt.dirDialog(parent:swt['shell'], c:[SWT.OPEN], text:'Open')
						fd.setFilterPath('D:\\')
						String path = fd.open()
						Text text = w['1-text']
						if(path)
							text.setText(path)
					})
				}
				w['1-text'] = text(c:[SWT.BORDER]){
					font(c:[fontType, fontSize, fontStyle])
					formData(top:[0, 3], left:[w['1-lbl'], 5], right:[w['1-btn'], -5])
					ctrla()
				}

				w['2-btn1'] = button(c:[SWT.PUSH], text:'加入-連結路徑'){
					font(c:[fontType, fontSize, fontStyle])
					//gridData(prop:[horizontalSpan:2])
					formData(top:[w['1-text'], 5], left:[0, 5])
					event(click:{
						Text path   = w['1-text']
						Text linked = w['3-t1']
						String pathStr   = path.text.trim()
						String linkedStr = linked.text.trim()
						if(StringUtils.isNotBlank(linkedStr))
							linkedStr += '\r\n'
						linkedStr += pathStr
						linked.text = linkedStr
					})
				}
				w['2-btn2'] = button(c:[SWT.PUSH], text:'加入-來源路徑'){
					font(c:[fontType, fontSize, fontStyle])
					formData(top:[w['1-text'], 5], left:[50, 5])
					event(click:{
						Text path   = w['1-text']
						Text linked = w['3-t2']
						String pathStr   = path.text.trim()
						String linkedStr = linked.text.trim()
						if(StringUtils.isNotBlank(linkedStr))
							linkedStr += '\r\n'
						linkedStr += pathStr
						linked.text = linkedStr
					})
				}

				w['4-btn1'] = button(c:[SWT.PUSH], text:'轉換'){
					font(c:[fontType, fontSize, fontStyle])
					formData(bottom:[100, -5], right:[100, -5])
					//gridData(prop:[horizontalAlignment:SWT.END, horizontalSpan:4])
					event(click:{
						Text linked = w['3-t1']
						Text source = w['3-t2']
						if(StringUtils.isBlank(linked.text.trim()) || StringUtils.isBlank(source.text.trim())){
							swt.alert(title:'警告', message:'無轉換路徑或來源')
							return
						}
						List<String> linkedList = SkUtils.str2List(linked.text.trim())
						List<String> sourceList = SkUtils.str2List(source.text.trim())
						if(linkedList.size() != sourceList.size()){
							swt.alert(title:'警告', message:'轉換路徑數量不符，不進行轉換')
						}

						for (int i = 0; i < linkedList.size(); i++) {
							String linkedP = linkedList.get(i)
							String sourceP = sourceList.get(i)
							MkLinkUtils.mkLink(new File(linkedP), new File(sourceP))
						}
						swt.alert(title:'結果', message:'轉換完成 !!')
					})
				}
				w['4-saveBtn'] = button(c:[SWT.PUSH], text:'儲存'){
					font(c:[fontType, fontSize, fontStyle])
					formData(bottom:[100, -5], left:[0, 5])
					event(click:{ SwtApplication.dialogSave(saveCB) })
				}
				w['4-loadBtn'] = button(c:[SWT.PUSH], text:'取出'){
					font(c:[fontType, fontSize, fontStyle])
					formData(bottom:[100, -5], left:[w['4-saveBtn'], 5])
					event(click:{
						SwtApplication.dialogGet(FUNC, { String value ->
							// get
							JsonSlurper js = new JsonSlurper()
							Map args = js.parseText(value)

							Text linked = w['3-t1']
							Text source = w['3-t2']
							linked.text = args.linked
							source.text = args.source
						}, { String key ->
							// save
							saveCB(key)
						}, { String key, String text->
							// rename
							FuncStorage.instance.rename(FUNC, key, text)
						}, { String key ->
							// delete
							FuncStorage.instance.delete(FUNC, key)
						})
					})
				}

				w['3-t1'] = text(c:[SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL]){
					font(c:[fontType, fontSize, fontStyle])
					//gridData(prop:[widthHint:500, heightHint:500, horizontalSpan:2])
					formData(top:[w['2-btn1'], 5], left:[0, 5], right:[50, -5], bottom:[w['4-btn1'], -5])
					ctrla()
				}
				w['3-t2'] = text(c:[SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL]){
					font(c:[fontType, fontSize, fontStyle])
					//gridData(prop:[widthHint:500, heightHint:500, horizontalSpan:2])
					formData(top:[w['2-btn1'], 5], left:[w['3-t1'], 10], right:[100, -5], bottom:[w['4-btn1'], -5])
					ctrla()
				}
			}
		}
	}
}
